const mongoose = require('mongoose');
const config = require('../config.js');

const { database: { host, port, dbname } } = config;

mongoose.connect(`mongodb://${host}:${port}/${dbname}`, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false,
});
mongoose.set('useCreateIndex', true);

const conn = mongoose.connection;
exports.plugin = {
  name: 'dbPlugin',
  register: async (server) => {
    conn.on('error', () => {
    });
    await conn.once('open', () => {
    });
    server.decorate('server', 'mongoose', mongoose);
  },
};
